package com.bpr.webservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpr.webservice.controller.util.Constants;
import com.bpr.webservice.domain.Users;
import com.bpr.webservice.domain.dto.UserDto;
import com.bpr.webservice.domain.dto.UserPageDto;
import com.bpr.webservice.repository.UsersRepository;

@Controller
@RequestMapping("/users")
public class UsersController {

	@Autowired UsersRepository usersRepository;

	@RequestMapping(value="/save", method = RequestMethod.POST )
	public ResponseEntity<String> doSave(
			@RequestBody Users user) { //Users user
		try {
			System.out.println(user);
			usersRepository.save(user);
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.SAVE_SUCCED,
				HttpStatus.OK);
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<String> doDelete(@PathVariable("id") Long id) {
		try {
			usersRepository.delete(id);
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.DELETE_SUCCED,
				HttpStatus.OK);
	}

	@RequestMapping(value = "/deletes", method = RequestMethod.POST)
	public ResponseEntity<String> doDeleteMoreThanOne(@RequestBody String[] ids) {
		try {
			for(int i=0;i < ids.length;i++){
				usersRepository.delete(Long.valueOf(ids[i]));
			}
		} catch (Exception ex) {
			return new ResponseEntity<String>(ex.getMessage(),
					HttpStatus.EXPECTATION_FAILED);
		}

		return new ResponseEntity<String>(Constants.CRUD_MESSAGE.DELETE_SUCCED,
				HttpStatus.OK);
	}
	
	@RequestMapping(value = "/findone/{id}", method = RequestMethod.GET)
	public @ResponseBody
	UserDto getOne(@PathVariable("id") Long id) {
		Users user = usersRepository.findByUserId(id);
		UserDto dto = new UserDto();
		if (user != null) {
			dto.constructDto(user);
		}

		return dto;
	}

	@RequestMapping(value = "/search", params={"page","limit"})
	public @ResponseBody
	Page<UserDto> findAll(@RequestParam(value="page") Integer page,@RequestParam(value="limit") Integer limit
			) {
		page = page -1;
		Page<Users> searchResult = usersRepository.findAll(new PageRequest(
				page, limit));
		Page<UserDto> dtoResult = new UserPageDto(searchResult);
		return dtoResult;
	}
	
	@RequestMapping(value = "/findByNameContains")
	public @ResponseBody Page<UserDto> findByNameContains(
			@RequestParam("name") String name,
			@RequestParam("page") Integer pagenum,
			@RequestParam("limit") Integer limit			
			){
		PageRequest page = new PageRequest(pagenum - 1, limit);
		Page<Users> searchResult = usersRepository.findByUsername(name + "%", page);
		Page<UserDto> dtoResult = new UserPageDto(searchResult);
		return dtoResult;
	}

	@RequestMapping(value = "/form.html")
	public String doForm() {

		return "users_form";
	}

	@RequestMapping(value = "/list.html")
	public String doList() {
		return "users_list";
	}
}
